package com.xingzhi.shortvideosharingplatform.controller;

import com.xingzhi.shortvideosharingplatform.common.Result;
import com.xingzhi.shortvideosharingplatform.dto.FavoritesFolderDTO;
import com.xingzhi.shortvideosharingplatform.model.FavoritesFolder;
import com.xingzhi.shortvideosharingplatform.service.FavoritesFolderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/favorites/folder")
public class FavoritesFolderController {

    @Autowired
    private FavoritesFolderService favoritesFolderService;
    
    /**
     * 创建收藏夹
     */
    @GetMapping("/create")
    public Result<Long> createFolder(@RequestParam Long userId, @RequestParam String folderName) {
        try {
            // TODO: 从Token中获取用户ID，此处暂时使用传入的userId
            Long folderId = favoritesFolderService.createFolder(userId, folderName);
            return Result.success(folderId, "创建收藏夹成功");
        } catch (Exception e) {
            log.error("创建收藏夹失败", e);
            return Result.error(500, "创建收藏夹失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新收藏夹信息
     */
    @PutMapping("/update")
    public Result<Boolean> updateFolder(@RequestBody FavoritesFolderDTO folderDTO) {
        try {
            // TODO: 从Token中获取用户ID并验证
            boolean success = favoritesFolderService.updateFolder(folderDTO);
            if (success) {
                return Result.success(true, "更新收藏夹成功");
            } else {
                return Result.error(400, "更新失败，收藏夹不存在或无权限操作");
            }
        } catch (Exception e) {
            log.error("更新收藏夹失败", e);
            return Result.error(500, "更新收藏夹失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除收藏夹
     */
    @DeleteMapping("/delete/{folderId}")
    public Result<Boolean> deleteFolder(@PathVariable Long folderId, @RequestParam Long userId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean success = favoritesFolderService.deleteFolder(folderId, userId);
            if (success) {
                return Result.success(true, "删除收藏夹成功");
            } else {
                return Result.error(400, "删除失败，收藏夹不存在或无权限操作");
            }
        } catch (Exception e) {
            log.error("删除收藏夹失败", e);
            return Result.error(500, "删除收藏夹失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户所有收藏夹
     */
    @GetMapping("/list")
    public Result<List<FavoritesFolder>> getUserFolders(@RequestParam Long userId) {
        try {
            // TODO: 从Token中获取用户ID
            List<FavoritesFolder> folders = favoritesFolderService.getUserFolders(userId);
            return Result.success(folders, "获取收藏夹列表成功");
        } catch (Exception e) {
            log.error("获取收藏夹列表失败", e);
            return Result.error(500, "获取收藏夹列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取收藏夹详情
     */
    @GetMapping("/detail/{folderId}")
    public Result<FavoritesFolder> getFolderDetail(@PathVariable Long folderId, @RequestParam Long userId) {
        try {
            // TODO: 从Token中获取用户ID并验证
            FavoritesFolder folder = favoritesFolderService.getFolderDetail(folderId, userId);
            if (folder != null) {
                return Result.success(folder, "获取收藏夹详情成功");
            } else {
                return Result.error(404, "收藏夹不存在或无权限访问");
            }
        } catch (Exception e) {
            log.error("获取收藏夹详情失败", e);
            return Result.error(500, "获取收藏夹详情失败: " + e.getMessage());
        }
    }
} 